package com.example.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.example.entity.College;
import com.example.entity.ZyzActivity;
import com.example.entity.ZyzSignUp;
import com.example.entity.ZyzStudent;
import com.example.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 志愿活动Service
 */
@Service
public class ZyzActivityService {
    @Autowired
    private ZyzActivityMapper zyzActivityMapper;
    @Autowired
    private CollegeMapper collegeMapper;
    @Autowired
    private ZyzStudentMapper zyzStudentMapper;
    @Autowired
    private ZyzSignUpMapper zyzSignUpMapper;
    @Autowired
    private ZyzCommentMapper zyzCommentMapper;

    /**
     * 新增
     * @param zyzActivity
     */
    public void add(ZyzActivity zyzActivity) {
        ZyzActivity zyzActivity1 = zyzActivityMapper.selectByName(zyzActivity.getName());
        if (ObjectUtil.isNotNull(zyzActivity1)){
            throw new RuntimeException("该活动已存在");
        }
        if (ObjectUtil.isEmpty(zyzActivity.getName())){
            throw new RuntimeException("活动名称不能为空");
        }
        if (ObjectUtil.isEmpty(zyzActivity.getDescription())){
            throw new RuntimeException("活动描述不能为空");
        }
        if (ObjectUtil.isEmpty(zyzActivity.getLocation())){
            throw new RuntimeException("活动地点不能为空");
        }
        if (ObjectUtil.isEmpty(zyzActivity.getOrganizer())){
            throw new RuntimeException("活动发起人不能为空");
        }
        if (ObjectUtil.isEmpty(zyzActivity.getImg())){
            throw new RuntimeException("活动图片不能为空");
        }
        if (ObjectUtil.isEmpty(zyzActivity.getSignUpStart()) || ObjectUtil.isEmpty(zyzActivity.getSignUpEnd())
        || ObjectUtil.isEmpty(zyzActivity.getActivityStart()) || ObjectUtil.isEmpty(zyzActivity.getActivityEnd())){
            throw new RuntimeException("活动报名时间和活动时间不能为空");
        }
        zyzActivityMapper.insert(zyzActivity);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    public int deleteBatch(String ids) {
        return zyzActivityMapper.deleteBatch(Convert.toStrArray(ids));
    }

    /**
     * 修改
     * @param zyzActivity
     */
    public void updateById(ZyzActivity zyzActivity) {
        zyzActivityMapper.updateById(zyzActivity);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ZyzActivity selectById(Integer id) {
        ZyzActivity zyzActivity = zyzActivityMapper.selectById(id);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format1 = format.format(zyzActivity.getSignUpStart());
        String format2 = format.format(zyzActivity.getSignUpEnd());
        String format3 = format.format(zyzActivity.getActivityStart());
        String format4 = format.format(zyzActivity.getActivityEnd());
        try {
            Date date1 = format.parse(format1);
            Date date2 = format.parse(format2);
            Date date3 = format.parse(format3);
            Date date4 = format.parse(format4);
            zyzActivity.setSignUpStart(date1);
            zyzActivity.setSignUpEnd(date2);
            zyzActivity.setActivityStart(date3);
            zyzActivity.setActivityEnd(date4);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String collegeIds = zyzActivity.getCollegeIds();
        if (ObjectUtil.isNotEmpty(collegeIds)){
            String name = "";
            String[] split = collegeIds.split(",");
            for (int i = 0; i < split.length; i++) {
                College college = collegeMapper.selectById(Integer.valueOf(split[i]));
                if (i == split.length - 1){
                    name += college.getName();
                    continue;
                }
                name += college.getName() + ",";
            }
            zyzActivity.setCollegeIds(name);
        }
        return zyzActivity;
    }

    /**
     * 查询所有
     * @param zyzActivity
     * @return
     */
    public List<ZyzActivity> selectAll(ZyzActivity zyzActivity) {
        if (!ObjectUtil.isEmpty(zyzActivity.getCollegeIds())){
            if (("限制").equals(zyzActivity.getCollegeIds())){
                zyzActivity.setCollegeIds("限制");
            }else if (("不限制").equals(zyzActivity.getCollegeIds())){
                zyzActivity.setCollegeIds("不限制");
            }
        }
        List<ZyzActivity> zyzActivities = zyzActivityMapper.selectAll(zyzActivity);
        for (ZyzActivity zyzActivity1 : zyzActivities) {
            String collegeIds = zyzActivity1.getCollegeIds();
            if (ObjectUtil.isNotEmpty(collegeIds)){
                String name = "";
                String[] split = collegeIds.split(",");
                for (int i = 0; i < split.length; i++) {
                    College college = collegeMapper.selectById(Integer.valueOf(split[i]));
                    if (i == split.length - 1){
                        name += college.getName();
                        continue;
                    }
                    name += college.getName() + ",";
                }
                zyzActivity1.setCollegeIds(name);
            }
            Integer count = zyzSignUpMapper.selectCount(zyzActivity1.getId());
            zyzActivity1.setSignUpCount(count);
            Integer commentCount = zyzCommentMapper.selectCount(zyzActivity1.getId());
            zyzActivity1.setCommentCount(commentCount);
        }

        return zyzActivities;
    }

    /**
     * 分页查询
     * @param zyzActivity
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<ZyzActivity> selectPage(ZyzActivity zyzActivity, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ZyzActivity> list = selectAll(zyzActivity);
        return PageInfo.of(list);
    }

    public Integer selectCount() {
        return zyzActivityMapper.selectCount();
    }

    public PageInfo<Map<String, Object>> selectCollegeCount(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<Map<String,Object>> list = zyzActivityMapper.selectCollegeCount();
        for (Map<String, Object> map : list){
            Object cid = map.get("cid");
            ZyzStudent zyzStudent = new ZyzStudent();
            zyzStudent.setCollegeId(Integer.parseInt(cid.toString()));
            List<ZyzStudent> zyzStudents = zyzStudentMapper.selectAll(zyzStudent);
            double num = 0.0;
            for (ZyzStudent zyzStudent1 : zyzStudents) {
                List<ZyzSignUp> list1 = zyzSignUpMapper.selectByStudentId(zyzStudent1.getId());
                if (ObjectUtil.isNotEmpty(list1)){
                    num += 1;
                }
            }
            Object studentcount = map.get("studentcount");
            num = num / Integer.parseInt(studentcount.toString());
            map.put("canyulv",num);
        }
        return PageInfo.of(list);
    }

    public List<Map<String, Object>> selectCollegeCount2() {
        List<Map<String,Object>> list = zyzActivityMapper.selectCollegeCount();
        for (Map<String, Object> map : list){
            Object cid = map.get("cid");
            ZyzStudent zyzStudent = new ZyzStudent();
            zyzStudent.setCollegeId(Integer.parseInt(cid.toString()));
            List<ZyzStudent> zyzStudents = zyzStudentMapper.selectAll(zyzStudent);
            double num = 0.0;
            for (ZyzStudent zyzStudent1 : zyzStudents) {
                List<ZyzSignUp> list1 = zyzSignUpMapper.selectByStudentId(zyzStudent1.getId());
                if (ObjectUtil.isNotEmpty(list1)){
                    num += 1;
                }
            }
            Object studentcount = map.get("studentcount");
            num = num / Integer.parseInt(studentcount.toString());
            map.put("canyulv",num);
        }
        return list;
    }
}
